﻿using System;
using System.Windows.Forms;
using System.Xml;

using Notepad_X.Classes;

namespace Notepad_X
{
    public partial class Options : Form
    {
        //Link to main window
        private Main main;

        //All download details as XML
        XmlDocument xmlDownloads;

        bool h_changed = false;

        //Latest version
        private int latestversion;

        //Latest version url string
        private string latesturl;


        private System.Collections.Generic.List<String> DirectoryList = new System.Collections.Generic.List<string>(2);

        public Options()
        {
            main = Notepad_X.Static.MainInstance ;
            InitializeComponent();
        }
        private bool listContatins(ListView list,string text){
            text=text.ToLower();
            foreach (ListViewItem item in list.Items)
            {
                if (item.Text.ToLower() == text)
                {
                    return true;
                }
            }
            return false;
        }
        private ListViewItem listGetItem(ListView list, string text){

            foreach (ListViewItem item in list.Items)
            {
                if (item.Text.ToLower() == text)
                {
                    return item;

                }
            }
            return null;
        }
        private void cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private string get_Shortcut(string name)
        {

            if (Main.app_settings.shortcuts.ContainsKey(name.ToLower()))
            {
                String ret = ((Notepad_X.Classes.shortcut)Main.app_settings.shortcuts[name.ToLower()]).keyString;
                ret = ret.Replace("|", " + ");
                ret = ret.Replace("control", "Ctrl");
                ret = ret.Replace("shift", "Shift");
                ret = ret.Replace("alt", "Alt");
                return ret;
            }
            else
            {
                return null;
            }
        }
        private void sh_selected()
        {
            sh_e_control.Checked = false;
            sh_e_shift.Checked = false;
            sh_e_alt.Checked = false;
            sh_e_label.Text = null;
            bool enabled = false;
            if (sh_list.SelectedItems.Count > 0)
            {
                enabled = true;
                ListViewItem selectedItem = sh_list.SelectedItems[0];
                String[] parser = selectedItem.SubItems[1].Text.ToLower().Split('+');

                for (int i = 0; i < parser.Length - 1; i++)
                {
                    switch (parser[i].Replace(" ", ""))
                    {
                        case "ctrl":
                            sh_e_control.Checked = true;
                            break;
                        case "shift":
                            sh_e_shift.Checked = true;
                            break;
                        case "alt":
                            sh_e_alt.Checked = true;
                            break;
                    }
                }
                string str = parser[parser.Length - 1].Replace(" ", "");
                sh_e_key.SelectedIndex = sh_e_key.FindStringExact(str);

                sh_e_label.Text = selectedItem.SubItems[0].Text;
                sh_e_label.Tag = selectedItem.Index;
                //sh_list.Enabled = false;
            }
            sh_e.Enabled = enabled;
            //sh_e_group.Enabled = enabled;
            //sh_e_apply.Enabled = enabled;
            //sh_e_clear.Enabled = enabled;
            //sh_e_cancel.Enabled = enabled;
        }
        private void List_CheckIcons(string filename)
        {
            i_list.ItemCheck -= new ItemCheckEventHandler(i_list_ItemCheck);

            //if ((filename || filename.ToLower() == Main.app_language[Strings.Default].ToLower())
            //{
            //    filename = "default";// main.app_language.strings["Default"].ToString().ToLower();
            //}
            
            foreach (ListViewItem item in i_list.Items)
            {
                if (item != null)
                {
                    if (item.SubItems[2].Text.ToLower() == filename)
                    {
                        item.Checked = true;
                    }
                    else
                    {
                        item.Checked = false;
                    }
                }
            }
            i_list.ItemCheck += new ItemCheckEventHandler(i_list_ItemCheck);
        }
        private void List_CheckLanguage(string filename)
        {
            l_list.ItemCheck -= new ItemCheckEventHandler(l_list_ItemCheck);
            filename=filename.ToLower();
            if ((filename == "en") || (filename == null) || (filename == "english"))
            {            
                filename = "english";
            }

                foreach (ListViewItem item in l_list.Items)
                {
                    if (item != null)
                    {
                        if (item.SubItems[4].Text.ToLower() == filename)
                        {
                            item.Checked = true;
                        }
                        else
                        {
                            item.Checked = false;
                        }
                    }
                 }
                l_list.ItemCheck += new ItemCheckEventHandler(l_list_ItemCheck);
                
           
        }
        //Download list add items
        private void dl_additems(string type)
        {
            dl_list.Items.Clear();

            foreach (XmlNode node in xmlDownloads.SelectNodes("Downloads/" + type + "/Item"))
            {
                ListViewItem newItem = new ListViewItem();
                newItem.Text = node.SelectSingleNode("name").InnerText;
                if ((type.ToLower() != "translations") && ( type.ToLower() != "highlighters"))
                {
                    newItem.SubItems.Add(node.SelectSingleNode("description").InnerText);
                }
          
                newItem.SubItems.Add(node.SelectSingleNode("author").InnerText);
                if ((type.ToLower() != "highlighters"))
                {
                    newItem.SubItems.Add(node.SelectSingleNode("version").InnerText);
                }
                newItem.SubItems.Add(node.SelectSingleNode("download").InnerText);
                newItem.SubItems.Add(node.SelectSingleNode("date").InnerText);
                dl_list.Items.Add(newItem);
            }
        }
        public void readDownloads(string path)
        {
            try {

                xmlDownloads = new XmlDocument();
                xmlDownloads.Load(path);

                dl_lastupdate.Text = xmlDownloads.SelectSingleNode("Downloads/Date").InnerText;
                latestversion = int.Parse(xmlDownloads.SelectSingleNode("Downloads/Latest/version").InnerText);
                u_latest.Text = xmlDownloads.SelectSingleNode("Downloads/Latest/name").InnerText;
                u_releasenotes.Text = xmlDownloads.SelectSingleNode("Downloads/Latest/changelog").InnerText.Replace("<li>", "# ").Replace("</li>", "");

                if (latestversion > Main.Version) {
                    u_dll.Enabled = true;
                    latesturl = xmlDownloads.SelectSingleNode("Downloads/Latest/directdownload").InnerText;
                    u_update_status.Text = "New version is available !";
                    u_update_status.ForeColor = System.Drawing.Color.Red;
                } else {
                    u_dll.Enabled = false;
                    u_update_status.Text = "No new version available";
                    u_update_status.ForeColor = System.Drawing.Color.Green;
                }


                dl_cat.Enabled = true;

                if (dl_cat.SelectedIndex == -1) {
                    dl_cat.SelectedIndex = dl_cat.FindStringExact(Main.app_language[Strings.Translation]);
                }

                dl_cat_SelectedIndexChanged(null, null);
            } catch (Exception e) {
                MessageBox.Show(e.Message);
                xmlDownloads = null;
                dl_cat.Enabled = false;
                dl_list.Clear();
                u_dll.Enabled = false;
            }
        }
        public void changeIcons(Button item, string key)
        {
            if ((key != null) && (main.IconsCollection.Images.ContainsKey(key)))
            {
                item.Image = main.IconsCollection.Images[key];
            }
        }
        public void changeIcons(Button item, Icons key)
        {
            if ((key != Icons.none) && (main.IconsCollection.Images.ContainsKey(key.ToString())))
            {
                item.Image = main.IconsCollection.Images[key.ToString()];
            }
        }
        private void Options_Load(object sender, EventArgs e)
        {
            if (main.IconsCollection.Images.ContainsKey("options"))
                this.Icon = System.Drawing.Icon.FromHandle(new System.Drawing.Bitmap(main.IconsCollection.Images["options"]).GetHicon());
            
            this.panel.ImageList = main.IconsCollection;
            tab_g.ImageKey = "options";
            tab_pi.ImageKey = "plugins";
            tab_tl.ImageKey = "toolbar";
            tab_h.ImageKey = "highlight";
            tab_sh.ImageKey = "shortcut";
            tab_dl.ImageKey = "download";
            tab_u.ImageKey = "update";
            tab_i.ImageKey = "icons";

            this.Text = Main.app_language[Strings.Options];
            _apply.Text = Main.app_language[Strings.Apply];
            _cancel.Text = Main.app_language[Strings.Cancel];

            font_preview.Text = Main.app_settings.font.Name + "  " + Main.app_settings.font.Size;
            if (font_preview.Text.Length > 19) font_preview.Text = font_preview.Text.Substring(0, 17) + "..";
            font_preview.Font = Main.app_settings.font;

            g_font.Text = Main.app_language[Strings.Font];

            tab_tl.Text = Main.app_language[Strings.Toolbars];
            tl_group.Text = Main.app_language[Strings.Toolbars];
            tl_up.Text = Main.app_language[Strings.Up];
            tl_down.Text = Main.app_language[Strings.Down];
            tl_r.Text = Main.app_language[Strings.Remove];
            tl_add.Text = Main.app_language[Strings.Add];
            tl_dll.Text = Main.app_language[Strings.Download];

            tl_i.Text = Main.app_language[Strings.SelectedInformation];
            tl_i_a_l.Text = Main.app_language[Strings.Author];
            tl_i_d_l.Text = Main.app_language[Strings.Description];
            tl_i_v_l.Text = Main.app_language[Strings.Version];
            tl_br.Text = Main.app_language[Strings.Browse];


            l_group.Text = Main.app_language[Strings.Language];
            l_add.Text = Main.app_language[Strings.Add];
            l_rem.Text = Main.app_language[Strings.Remove];
            l_dll.Text = Main.app_language[Strings.Download];

            tab_h.Text = Main.app_language[Strings.Highlighters];
            h_add.Text = Main.app_language[Strings.Add];
            h_rem.Text = Main.app_language[Strings.Remove];
            h_dll.Text = Main.app_language[Strings.Download];
            h_br.Text = Main.app_language[Strings.Browse];


            tab_sh.Text = Main.app_language[Strings.Shortcuts];
            sh_list.Columns[0].Text = Main.app_language[Strings.ItemName];
            sh_list.Columns[1].Text = Main.app_language[Strings.Shortcut];
            sh_e_group.Text = Main.app_language[Strings.Shortcut];
            sh_edit.Text = Main.app_language[Strings.Edit];
            sh_e_apply.Text = Main.app_language[Strings.Apply];
            sh_e_clear.Text = Main.app_language[Strings.Clear];
            sh_e_cancel.Text = Main.app_language[Strings.Cancel];
            sh_e_label.Text = null;
            sh_default.Text = Main.app_language[Strings.Default];

            tab_pi.Text = Main.app_language[Strings.Plugins];
            pi_group.Text = Main.app_language[Strings.Plugins];
            pi_add.Text = Main.app_language[Strings.Add];
            pi_a.Text = Main.app_language[Strings.Activate];
            pi_d.Text = Main.app_language[Strings.Deactivate];
            pi_r.Text = Main.app_language[Strings.Remove];
            pi_s.Text = Main.app_language[Strings.Options];
            pi_i.Text = Main.app_language[Strings.SelectedInformation];
            pi_i_a_l.Text = Main.app_language[Strings.Author];
            pi_i_d_l.Text = Main.app_language[Strings.Description];
            pi_i_v_l.Text = Main.app_language[Strings.Version];
            pi_br.Text = Main.app_language[Strings.Browse];
            pi_dll.Text = Main.app_language[Strings.Download];

            tab_dl.Text = Main.app_language[Strings.Download];
            dl_cat_l.Text = Main.app_language[Strings.Category];
            dl_download.Text = Main.app_language[Strings.Download];
            dl_update.Text = Main.app_language[Strings.Update];
            tl_list_c_n.Text = Main.app_language[Strings.Name];
            tl_list_c_d.Text = Main.app_language[Strings.Description];
            tl_list_c_a.Text = Main.app_language[Strings.Author];
            tl_list_c_v.Text = Main.app_language[Strings.Version];
            dl_group.Text= Main.app_language[Strings.SelectedInformation];
            dl_n_l.Text = Main.app_language[Strings.Name];
            dl_d_l.Text =Main.app_language[Strings.Description];

            tab_u.Text = Main.app_language[Strings.Update];
            u_update.Text = Main.app_language[Strings.Update];
            u_dll.Text = Main.app_language[Strings.Download];
            u_latest_label.Text = Main.app_language[Strings.LatestVersion];
            u_current_label.Text = Main.app_language[Strings.CurrentVersion];
            u_releasenotes_label.Text = Main.app_language[Strings.ReleaseNotes];


            tab_g.Text = Main.app_language[Strings.General];
            g_group.Text = Main.app_language[Strings.General];
            g_statusBar.Text = Main.app_language[Strings.StatusBar];

            tab_i.Text = Main.app_language[Strings.Icons];
            i_br.Text = Main.app_language[Strings.Browse];
            i_add.Text = Main.app_language[Strings.Add];
            i_dll.Text = Main.app_language[Strings.Download];
            i_rem.Text = Main.app_language[Strings.Remove];

            i_list.Columns[0].Text =Main.app_language[Strings.Name];
            i_list.Columns[1].Text = Main.app_language[Strings.Author];
            i_i_group.Text = Main.app_language[Strings.SelectedInformation];
            i_i_d_label.Text = Main.app_language[Strings.Description];
            i_i_v_label.Text = Main.app_language[Strings.Version];
            i_i_l_label.Text = Main.app_language[Strings.Website];

            e_group.Text = Main.app_language[Strings.Extensions];

            e_s_add.Text = Main.app_language[Strings.Add];
            e_s_edit.Text = Main.app_language[Strings.Edit];
            e_s_rem.Text = Main.app_language[Strings.Remove];
            e_s_list.Columns[0].Text = Main.app_language[Strings.Description];
            e_s_list.Columns[1].Text = Main.app_language[Strings.Extensions];
            e_o_label.Text = Main.app_language[Strings.Save];
            e_o_add.Text = Main.app_language[Strings.Add];
            e_o_edit.Text = Main.app_language[Strings.Edit];
            e_o_rem.Text = Main.app_language[Strings.Remove];
            e_o_list.Columns[0].Text = Main.app_language[Strings.Description];
            e_o_list.Columns[1].Text = Main.app_language[Strings.Extensions];
            e_o_label.Text = Main.app_language[Strings.Open];
            e_s_label.Text = Main.app_language[Strings.Save];

            u_current.Text = Main.VersionName;
            DirectoryList.Clear();
            if(main.useAppDataFolder)DirectoryList.Add(main.System_AppData_FOLDER);
            DirectoryList.Add(Main.System_ROOT);

            g_editor_select.Items.Clear();
            g_editor_select.Items.AddRange(DocumentTypes.getNames());
            g_editor_select.Enabled = false;
            if (g_editor_select.Items.Count > 1) {
                g_editor_select.Enabled = true;
            }
            g_editor_select.SelectedIndex  = Main.app_settings.defaultDocumentType;
            // Toolbars
            if (main.useAppDataFolder)
            {
                if (!System.IO.Directory.Exists(main.System_AppData_FOLDER + "\\Toolbars\\")) System.IO.Directory.CreateDirectory(main.System_AppData_FOLDER + "\\Toolbars\\");
                if (!System.IO.Directory.Exists(main.System_AppData_FOLDER + "\\Plugins\\")) System.IO.Directory.CreateDirectory(main.System_AppData_FOLDER + "\\Plugins\\");
                if (!System.IO.Directory.Exists(main.System_AppData_FOLDER + "\\Translations\\")) System.IO.Directory.CreateDirectory(main.System_AppData_FOLDER + "\\Translations\\");
                if (!System.IO.Directory.Exists(main.System_AppData_FOLDER + "\\Icons\\")) System.IO.Directory.CreateDirectory(main.System_AppData_FOLDER + "\\Icons\\");
            }else{
                if (!System.IO.Directory.Exists(Main.System_ROOT + "\\Toolbars\\")) System.IO.Directory.CreateDirectory(Main.System_ROOT + "\\Toolbars\\");
                if (!System.IO.Directory.Exists(Main.System_ROOT + "\\Plugins\\")) System.IO.Directory.CreateDirectory(Main.System_ROOT + "\\Plugins\\");
                if (!System.IO.Directory.Exists(Main.System_ROOT + "\\Translations\\")) System.IO.Directory.CreateDirectory(Main.System_ROOT + "\\Translations\\");
                if (!System.IO.Directory.Exists(Main.System_ROOT + "\\Icons\\")) System.IO.Directory.CreateDirectory(Main.System_ROOT + "\\Icons\\");
            }
            tl_list.Items.Clear();
            int count = 0;
            foreach(string Dir in DirectoryList){
                if(System.IO.Directory.Exists(Dir + "\\Toolbars\\"))
                    foreach (System.IO.FileInfo file in (new System.IO.DirectoryInfo(Dir + "\\Toolbars\\").GetFiles("*.xml")))
                    {
                        string[] hash = Toolbar.hashToolbar(file.FullName);
                        if (hash != null)
                        {
                            ListViewItem newItem = new ListViewItem();
                            newItem.Name = hash[0]; //name
                            newItem.Text = hash[0];
                            newItem.SubItems.Add(file.Name); //path
                            newItem.SubItems.Add(hash[1]); //author 
                            newItem.SubItems.Add(hash[2]); //version 
                            newItem.SubItems.Add(hash[3]); //description 
                            tl_list.Items.Add(newItem);
                        }
                    }
            }
            count=0;
            Boolean[] Flags = new Boolean[Main.app_settings.toolbars.Count];
            for (int i = 0; i < Main.app_settings.toolbars.Count; i++) Flags[i] = true;

            for (int i = 0; i < Main.app_settings.toolbars.Count; i++)
            {
                int min = 0;
                while (!Flags[min])
                {
                    min++;
                }
                for (int j = 1; j < Main.app_settings.toolbars.Count; j++)
                {
                    if (Flags[j] && ((Toolbar)Main.app_settings.toolbars[j]).DisplayIndex < ((Toolbar)Main.app_settings.toolbars[min]).DisplayIndex)
                    {
                        min = j;
                    }
                }
                Flags[min] = false;
                string Name = ((Notepad_X.Classes.Toolbar)Main.app_settings.toolbars[min]).Name;
                for (int j = 0; j < tl_list.Items.Count; j++)
                {
                    if (tl_list.Items[j].Text == Name)
                    {
                        tl_list.Items[j].Checked = true;
                        int index = j;
                        while (index > count)
                        {
                            ListViewItem tempitem = tl_list.Items[index];
                            ListViewItem tempitem2 = tl_list.Items[index - 1];

                            tl_list.Items.RemoveAt(index);
                            tl_list.Items.RemoveAt(index - 1);
                            tl_list.Items.Insert(index - 1, tempitem);
                            tl_list.Items.Insert(index, tempitem2);

                            index--;
                        }
                        break;
                    }
                }
                count++;
            }
            
            // plugins
            pi_list.Items.Clear();
            foreach (string Dir in DirectoryList) {
                if (System.IO.Directory.Exists(Dir + "\\Plugins\\"))
                    foreach (System.IO.FileInfo file in (new System.IO.DirectoryInfo(Dir + "\\Plugins\\").GetFiles("*.dll"))) {
                        string[] hash = Notepad_X.Plugin.Global.Plugins.hash(file.FullName);

                        if (hash != null) {
                            ListViewItem newItem = new ListViewItem();
                            newItem.Name = hash[0]; //name
                            newItem.Text = hash[0]; //name
                            newItem.SubItems.Add(file.FullName);
                            newItem.SubItems.Add(hash[1]); //author
                            newItem.SubItems.Add(hash[2]); //version
                            newItem.SubItems.Add(hash[3]); //description

                            pi_list.Items.Add(newItem);
                            if (Notepad_X.Plugin.Global.Plugins.AvailablePlugins.Exist(file.FullName)) {
                                newItem.Checked = true;
                            }

                        }
                    }
            }

             //Language
            l_list.Items.Clear();

            ListViewItem eItem = new ListViewItem();
                eItem.Text = "English";
                eItem.SubItems.Add("English");
                eItem.SubItems.Add("NohponeX"); //author 
                eItem.SubItems.Add("1"); //version
                eItem.SubItems.Add("English");
                l_list.Items.Add(eItem);

            foreach (string Dir in DirectoryList) {
                if (System.IO.Directory.Exists(Dir + "\\Translations"))
                    foreach (System.IO.FileInfo file in (new System.IO.DirectoryInfo(Dir + "\\Translations").GetFiles("*.xml"))) {
                        string[] hash = Language.hashTranslation(file.FullName);
                        if (hash != null) {
                            ListViewItem newItem = new ListViewItem();
                            newItem.Text = hash[0];
                            newItem.SubItems.Add(file.FullName);//path
                            newItem.SubItems.Add(hash[1]); //author 
                            newItem.SubItems.Add(hash[2]); //version
                            newItem.SubItems.Add(file.Name);//filename
                            l_list.Items.Add(newItem);
                        }
                    }
            }
            List_CheckLanguage(Main.app_settings.language);

            // icons
            i_list.Items.Clear();


            //ListViewItem icon_default = new ListViewItem(Main.app_language[Strings.Default]);
            //icon_default.SubItems.AddRange(new string[5] { "Default", "Default", "1", "Default Icons", "" });
            //i_list.Items.Add(icon_default);
            //icon_default.Checked = true;
            //icon_default.SubItems.Add(main.app_language.strings["Default"].ToString());

            foreach (string Dir in DirectoryList) {
                if (System.IO.Directory.Exists(Dir + "\\Plugins\\"))
                    foreach (System.IO.FileInfo file in (new System.IO.DirectoryInfo(Dir + "\\Icons\\").GetFiles("*.pack"))) {
                        string packfile = System.IO.Path.GetFileNameWithoutExtension(file.FullName);
                        if (!System.IO.File.Exists(Dir + "\\Icons\\" + packfile + ".xml")) {
                            if (Packages.Unpack(file.FullName, Dir + "\\Icons")) {
                                System.IO.File.Delete(file.FullName);
                            }
                        }
                    }
            }
            foreach (string Dir in DirectoryList) {
                if (System.IO.Directory.Exists(Dir + "\\Plugins\\"))
                    foreach (System.IO.FileInfo file in (new System.IO.DirectoryInfo(Dir + "\\Icons\\").GetFiles("*.xml"))) {
                        string[] hash = Settings.hashIconPack(file.FullName);
                        if (hash != null) {
                            ListViewItem newItem = new ListViewItem();
                            newItem.Name = hash[0]; //name
                            newItem.Text = hash[0];
                            newItem.SubItems.Add(hash[1]); //author
                            newItem.SubItems.Add(file.Name); //path
                            newItem.SubItems.Add(hash[2]); //version 
                            newItem.SubItems.Add(hash[3]); //description 
                            newItem.SubItems.Add(hash[4]); //link 
                            i_list.Items.Add(newItem);
                        }
                    }
            }
            List_CheckIcons(Main.app_settings.icons);
            //Shortcuts

            // listitem {label}[0] {key}[1] !hidden {control_name}[2]


            //sh_e_key.Items.AddRange(Enum.GetNames(typeof(Keys)));
            sh_list.Items.Clear();
            foreach (ToolStripItem item in main.Menu_.Items)
            {
                if ((item.GetType() == typeof(ToolStripMenuItem))||(item.GetType() == typeof(Notepad_X.Components.MenuItemX)))
                {
                    ListViewGroup newGroup = new ListViewGroup(item.Text);
                    sh_list.Groups.Add(newGroup);
                    foreach (ToolStripItem subtitem in ((ToolStripMenuItem)item).DropDownItems)
                    {
                        if ((subtitem.GetType() == typeof(ToolStripMenuItem)) || (subtitem.GetType() == typeof(Notepad_X.Components.MenuItemX)))
                        {
                            ListViewItem newItem = new ListViewItem();
                            newItem.Text = subtitem.Text;
                            newItem.SubItems.Add(get_Shortcut(subtitem.Name));
                            newItem.SubItems.Add(subtitem.Name);
                            newItem.Group = newGroup;
                            sh_list.Items.Add(newItem);
                            foreach (ToolStripItem sub2item in ((ToolStripMenuItem)subtitem).DropDownItems)
                            {
                                if ((sub2item.GetType() == typeof(ToolStripMenuItem)) || (sub2item.GetType() == typeof(Notepad_X.Components.MenuItemX)))
                                {
                                    ListViewItem newSubItem = new ListViewItem();
                                    newSubItem.Text = sub2item.Text;
                                    newSubItem.SubItems.Add(get_Shortcut(sub2item.Name));
                                    newSubItem.SubItems.Add(sub2item.Name);
                                    newSubItem.Group = newGroup;
                                    sh_list.Items.Add(newSubItem);
                                }
                            }
                        }
                    }
                }
            }           
            //downloads
            dl_cat.Items[0] = Main.app_language[Strings.Translation];
            dl_cat.Items[1] = Main.app_language[Strings.Toolbars];
            dl_cat.Items[2] = Main.app_language[Strings.Highlighters];
            dl_cat.Items[3] = Main.app_language[Strings.Plugins];
            dl_cat.Items[4] = Main.app_language[Strings.Icons];

            dl_lastupdate.Text = null;
            foreach (string Dir in DirectoryList) {
                if (System.IO.File.Exists(Dir + "\\downloads.xml")) {
                    readDownloads(Dir + "\\downloads.xml");
                    break;
                }
            }

            g_statusBar.Checked = Main.app_settings.status_bar;

            g_liteVersion.Checked = Main.app_settings.LiteVersion;

            if (Main.app_settings.BackgroundColor == null) {
                g_backgroundColor.Checked = true;
                g_backgroundColor_preview.BackColor = System.Drawing.SystemColors.Window;
                g_backgroundColor_choose.Enabled=false;
            }else{
                g_backgroundColor_preview.BackColor = Settings.ParseColor(Main.app_settings.BackgroundColor);
                g_backgroundColor.Checked = false;
                g_backgroundColor_choose.Enabled=true;
            }

            if (Main.app_settings.TextColor == null) {
                g_fontColor.Checked = true;
                g_fontColor_preview.BackColor = System.Drawing.SystemColors.WindowText;
                g_fontColor_choose.Enabled = false;
            } else {
                g_fontColor_preview.BackColor = Settings.ParseColor(Main.app_settings.TextColor);
                g_fontColor.Checked = false;
                g_fontColor_choose.Enabled = true;
            }

            //Highlighters
            updateHightlightersList();

            //extensions
            e_s_list.Items.Clear();
            

           
            try
            {
                if (!String.IsNullOrEmpty(Main.app_settings.extension_save))
                {
                    string[] ext = Main.app_settings.extension_save.Split('|');
                    if (ext.Length >= 2)
                    {
                        for (int i = 0; i < ext.Length; i += 2)
                        {
                            ListViewItem newItem = new ListViewItem();
                            newItem.Text = ext[i];
                            newItem.SubItems.Add(ext[i + 1]);
                            e_s_list.Items.Add(newItem);
                        }
                    }
                }

            }
            catch{}

            e_o_list.Items.Clear();
            try
            {
                if (!String.IsNullOrEmpty(Main.app_settings.extension_open))
                {
                    String[] ext = Main.app_settings.extension_open.Split('|');
                    if (ext.Length >= 2)
                    {
                        for (int i = 0; i < ext.Length; i += 2)
                        {
                            ListViewItem newItem = new ListViewItem();
                            newItem.Text = ext[i];
                            newItem.SubItems.Add(ext[i + 1]);
                            e_o_list.Items.Add(newItem);
                        }
                    }
                }
            }
            catch
            {
            }

            if (!String.IsNullOrEmpty(Main.app_settings.icons))
            {
                changeIcons(_apply, "apply");
                changeIcons(_cancel, "cancel");
                changeIcons(l_dll, "download");
                changeIcons(l_add, "add");
                changeIcons(l_rem, "remove");
                changeIcons(tl_dll, "download");
                changeIcons(tl_add, "add");
                changeIcons(tl_up, "up");
                changeIcons(tl_r, "remove");
                changeIcons(tl_down, "down");
                changeIcons(tl_br, "open");
                changeIcons(h_dll, "download");
                changeIcons(h_add, "add");
                changeIcons(h_rem, "remove");
                changeIcons(h_br, "open");

                changeIcons(sh_e_apply, "apply");
                changeIcons(sh_e_cancel, "cancel");
                changeIcons(sh_e_clear, "clear");
                changeIcons(sh_default, "refresh");

                changeIcons(u_update, "refresh");
                changeIcons(u_dll, "download");

                changeIcons(pi_add, "add");
                changeIcons(pi_a, "activate");
                changeIcons(pi_d, "deactivate");
                changeIcons(pi_s, "options");
                changeIcons(pi_br, "open");
                changeIcons(pi_dll, "download");

                changeIcons(i_add, "add");
                changeIcons(i_rem, "remove");
                changeIcons(i_br, "open");
                changeIcons(i_dll, "download");
                
                changeIcons(dl_update, "refresh");
                changeIcons(dl_download, "download");

                changeIcons(e_s_add, "add");
                changeIcons(e_s_edit, "edit");
                changeIcons(e_s_rem, "remove");
                
                changeIcons(e_o_add, "add");
                changeIcons(e_o_edit, "edit");
                changeIcons(e_o_rem, "remove");
            }
        }
        private void updateHightlightersList()
        {
            h_list.Items.Clear();

            string key;
            foreach (string Dir in DirectoryList) {
                if (System.IO.Directory.Exists(Dir + "\\Highlighters\\"))
                    foreach (string file in System.IO.Directory.GetFiles(Dir + "\\Highlighters\\")) {
                        if (System.IO.Path.GetExtension(file).ToLower() == ".xshd") {
                            key = highlighter_check(file);
                            if (key != null) {
                                ListViewItem newItem = new ListViewItem();
                                newItem.Text = key;
                                newItem.SubItems.Add(file);
                                h_list.Items.Add(newItem);
                            }
                        }
                    }
            }
            //foreach (System.Collections.DictionaryEntry item in ICSharpCode.TextEditor.Document.HighlightingManager.Manager.HighlightingDefinitions)
            //{
            //    key = item.Key.ToString();
            //    if (key.ToLower() != "default")
            //    {
            //        ListViewItem newItem = new ListViewItem();
            //        newItem.Text = item.Value.ToString();
            //        newItem.SubItems.Add(key);
            //        h_list.Items.Add(newItem);
            //    }
            //}
        }

        public string highlighter_check(string path){
            try
            {
                XmlDocument xmlFile = new XmlDocument();
                xmlFile.Load(path);

                string i_name = xmlFile.SelectSingleNode("SyntaxDefinition").Attributes[0].InnerText;
                //string i_extensions = xmlFile.SelectSingleNode("SyntaxDefinition").Attributes[1].InnerText;
                return i_name;
            }
            catch
            {
                return null;
            }

        }
        private void sh_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            sh_selected();
        }

        private void sh_e_cancel_Click(object sender, EventArgs e)
        {
            sh_list.SelectedItems[0].Selected = false;
            sh_list.Enabled = true;
            sh_e.Enabled = false;
        }

        private void sh_e_apply_Click(object sender, EventArgs e)
        {
            //validate
            if (((sh_e_control.Checked) || (sh_e_shift.Checked) || (sh_e_alt.Checked))&& sh_e_key.SelectedItem!=null)
            {
                string str = "";
                if (sh_e_control.Checked) { str += "Ctrl + "; };
                if (sh_e_shift.Checked ) { str += "Shift + "; };
                if (sh_e_alt.Checked) { str += "Alt + "; };
                str += sh_e_key.SelectedItem.ToString();
                sh_list.Items[int.Parse(sh_e_label.Tag.ToString())].SubItems[1].Text = str;
                sh_list.Enabled = true;
            }
        }
        private bool isChecked(ListView Control,String text)
        {
            text = text.ToLower();
            foreach(ListViewItem Item in Control.Items){
                if (Item.Text.ToLower() == text)
                {
                    if (Item.Checked)
                    {
                        return true; 
                    }
                }
            }
            return false;
        }
        private void apply_click(object sender, EventArgs e)
        {
            Main.app_settings.font = font_preview.Font;

            Main.app_settings.defaultDocumentType = g_editor_select.SelectedIndex;

            foreach (ListViewItem item in sh_list.Items)
            {
                if (!String.IsNullOrEmpty(item.SubItems[1].Text))
                {
                    //shortcutkey = new shortcutkey(
                    bool sh_control = false;
                    bool sh_shift = false;
                    bool sh_alt = false;
                    string[] parser = item.SubItems[1].Text.Split('+');
                    for (int i = 0; i < parser.Length - 1; i++)
                    {
                        switch (parser[i].ToLower().Replace(" ", ""))
                        {

                            case "ctrl":
                                sh_control = true;
                                break;
                            case "shift":
                                sh_shift = true;
                                break;
                            case "alt":
                                sh_alt = true;
                                break;
                        }
                    }
                    KeysConverter _converter = new KeysConverter();
                    Keys _key = (Keys)_converter.ConvertFromString(parser[parser.Length - 1].Replace(" ", ""));
                    shortcutkey newKey = new shortcutkey(sh_control, sh_shift, sh_alt, _key);

                    if (Main.app_settings.shortcuts.ContainsKey(item.SubItems[2].Text.ToLower()))
                    {

                        ((shortcut)Main.app_settings.shortcuts[item.SubItems[2].Text.ToLower()]).key = newKey.key;
                        ((shortcut)Main.app_settings.shortcuts[item.SubItems[2].Text.ToLower()]).keyString = newKey.getString;

                    }
                    else
                    {
                        ToolStripMenuItem TSitem = main.system_menuItem_Get(item.SubItems[2].Text);
                        if (item != null)
                            Main.app_settings.shortcuts[item.SubItems[2].Text.ToLower()] = new shortcut(TSitem, newKey.key, newKey.getString);
                    }
                }  
            }
            //Toolbars
            System.Collections.Generic.List<Toolbar> ToolbarsToDelete = new System.Collections.Generic.List<Toolbar>();
            foreach(Toolbar item in Main.app_settings.toolbars){
                 if(!isChecked(tl_list,item.Name)){
                     item.Delete();
                     ToolbarsToDelete.Add(item);
                 }
            }
            foreach (Toolbar t in ToolbarsToDelete)
            {
                Main.app_settings.toolbars.Remove(t);
            }
            ToolbarsToDelete.Clear();
            ToolbarsToDelete = null;


            int count=0;
            for (int i = 0; i < tl_list.Items.Count; i++)
            {
                if (tl_list.Items[i].Checked){
                     if (!Main.app_settings.toolbar_exist(tl_list.Items[i].Text)){
                           Main.app_settings.toolbar_add(tl_list.Items[i].SubItems[1].Text);
                      }
                      Main.app_settings.toolbar_get(tl_list.Items[i].Text).DisplayIndex=count;
                     }
                count++;
            }

            for (int i = tl_list.Items.Count - 1; i >= 0; i--)
            {
                if (tl_list.Items[i].Checked)
                {
                    Main.app_settings.toolbar_get(tl_list.Items[i].Text).nToolbar.SendToBack();
                }
            }
            main.Menu_.SendToBack();

            //Language
            string language = null; string language_file = null;
            foreach (ListViewItem item in l_list.Items)
            {
                if (item.Checked)
                {
                    language_file = item.SubItems[1].Text;
                    language = item.SubItems[4].Text;
                    break;
                }
            }

            //Icons
            string icons = null;
            foreach (ListViewItem item in i_list.Items)
            {
                if (item.Checked)
                {
                    icons = item.SubItems[2].Text.ToLower();
                    break;
                }
            }
            bool bChangedIicons = false;

            if (Main.app_settings.icons != icons)
            {
                Main.app_settings.icons = icons;

                if (String.IsNullOrEmpty(icons))
                {
                    System.Windows.Forms.MessageBox.Show("Icons will reset after the restart");
                }
                else
                {
                    Main.app_settings.iconReader(icons);
                    bChangedIicons = true;
                }

            }
            if (language.ToLower() != Main.app_language.language.ToLower()||(bChangedIicons))
                {
                if (language.ToLower() == "english")
                {
                    Main.app_language.set_default();
                    Main.app_settings.language = "english";
                }
                else
                {
                    Main.app_language.ReadLanguageFile(language_file);
                    Main.app_settings.language = language;
                }
                main.UpdateControls(bChangedIicons);
            }

            //Plugins
            foreach (ListViewItem item in pi_list.Items)
            {
                if (item.Checked)
                {
                    if (!Notepad_X.Plugin.Global.Plugins.AvailablePlugins.Exist(item.SubItems[1].Text))
                    {
                        Notepad_X.Plugin.Global.Plugins.AddPlugin(item.SubItems[1].Text);
                    }
                }else{
                     if (Notepad_X.Plugin.Global.Plugins.AvailablePlugins.Exist(item.SubItems[1].Text))
                     {
                         Notepad_X.Plugin.Global.Plugins.ClosePlugin(item.SubItems[1].Text);
                     }
                    
                }
            }
            foreach (Notepad_X.Plugin.AvailablePlugin item in Notepad_X.Plugin.Global.Plugins.AvailablePlugins)
            {
                bool found = false;
                foreach (ListViewItem listItem in pi_list.Items)
                {
                    if (item.AssemblyPath.ToLower() == listItem.SubItems[1].Text.ToLower())
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    Notepad_X.Plugin.Global.Plugins.ClosePlugin(item.AssemblyPath);
                }
            }
            //Hightlighters
            if (h_changed)
            {
                main.highlighterItems();
            }
            string saveExtensions = "";
            foreach (ListViewItem item in e_s_list.Items)
            {
                saveExtensions += item.Text + "|" + item.SubItems[1].Text + "|";
            }
            if (saveExtensions.EndsWith("|"))
            {
                saveExtensions=saveExtensions.Remove(saveExtensions.Length - 1, 1);
            }
            string openExtensions = "";
            foreach (ListViewItem item in e_o_list.Items)
            {
                openExtensions += item.Text + "|" + item.SubItems[1].Text + "|";
            }
            if(openExtensions.EndsWith("|")){
                openExtensions=openExtensions.Remove(openExtensions.Length - 1, 1);
            }
            Main.app_settings.extension_save = saveExtensions;
            Main.app_settings.extension_open = openExtensions;
            Main.app_settings.status_bar = g_statusBar.Checked;
            Main.app_settings.LiteVersion = g_liteVersion.Checked;
            if (g_backgroundColor.Checked) { Main.app_settings.BackgroundColor = null;
            } else {
                if (g_backgroundColor_preview.BackColor.IsKnownColor) {
                    Main.app_settings.BackgroundColor = "color." + g_backgroundColor_preview.BackColor.Name;
                } else {
                    Main.app_settings.BackgroundColor = g_backgroundColor_preview.BackColor.R + "." + g_backgroundColor_preview.BackColor.G + "." + g_backgroundColor_preview.BackColor.B;
                }
            }
            if (g_fontColor.Checked) {
                Main.app_settings.TextColor = null;
            } else {
                if (g_fontColor_preview.BackColor.IsKnownColor) {
                    Main.app_settings.TextColor = "color." + g_fontColor_preview.BackColor.Name;
                } else {
                    Main.app_settings.TextColor = g_fontColor_preview.BackColor.R + "." + g_fontColor_preview.BackColor.G + "." + g_fontColor_preview.BackColor.B;
                }
            }
            main.app_SetSettings();
            this.Close();
        }

        private void tl_list_SelectedIndexChanged(object sender, EventArgs e)
        {
             bool enabled = false;
             if (tl_list.SelectedItems.Count > 0)
             {
                 enabled = true;
                 ListViewItem selectedItem = tl_list.SelectedItems[0];
                 tl_i_a.Text = selectedItem.SubItems[2].Text;
                 tl_i_d.Text = selectedItem.SubItems[4].Text;
                 tl_i_v.Text = selectedItem.SubItems[3].Text;
             }else{
                 tl_i_a.Text = "";
                 tl_i_d.Text = "";
                 tl_i_v.Text = "";
             }
             tl_r.Enabled = enabled;
             tl_up.Enabled = enabled;
             tl_down.Enabled = enabled;
             tl_r.Enabled = enabled;

             tl_i.Visible = enabled;
             if ((enabled) && (tl_list.SelectedItems[0].Index == 0))
             {
                 tl_up.Enabled = false;
             }
             if ((enabled) && (tl_list.SelectedItems[0].Index == tl_list.Items.Count-1))
             {
                 tl_down.Enabled = false;
             }
             //if ((enabled == true) && (tl_list.SelectedItems[0].Checked == true))
             //{
             //    tl_a.Enabled = false;
             //}
             //if ((enabled == true) && (tl_list.SelectedItems[0].Checked == false))
             //{
             //    tl_d.Enabled = false;
             //}

            
        }

        private void font_select_Click(object sender, EventArgs e)
        {
            FontDialog dialog = new FontDialog();
            dialog.FontMustExist = true;
            dialog.Font = font_preview.Font;
            dialog.ShowColor = false;
            dialog.ShowEffects = false;
            if(dialog.ShowDialog()== DialogResult.OK){
                font_preview.Font = dialog.Font;
                font_preview.Text = dialog.Font.Name + "  " + dialog.Font.Size;
                if (font_preview.Text.Length > 19) font_preview.Text = font_preview.Text.Substring(0, 17) + "..";
            }
        }

        private void l_a_Click(object sender, EventArgs e)
        {
            if (l_list.SelectedItems.Count > 0)
            {
                List_CheckLanguage(l_list.SelectedItems[0].SubItems[4].Text);
            }
        }


        private void l_list_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            List_CheckLanguage(l_list.Items[e.Index].SubItems[4].Text);
        }
        private void i_list_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            List_CheckIcons(i_list.Items[e.Index].SubItems[1].Text);
        }

        private void pi_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool enabled = false;
            if (pi_list.SelectedItems.Count > 0)
            {
                enabled = true;
                ListViewItem selectedItem = pi_list.SelectedItems[0];
                pi_i_a.Text = selectedItem.SubItems[2].Text;
                pi_i_d.Text = selectedItem.SubItems[4].Text;
                pi_i_v.Text = selectedItem.SubItems[3].Text;
            }else{
                pi_i_a.Text = "";
                pi_i_d.Text = "";
                pi_i_v.Text = "";
            }
            pi_s.Enabled = false;
            pi_a.Enabled = enabled;
            pi_d.Enabled = enabled;
            pi_r.Enabled = enabled;
            pi_i.Visible = enabled;
            if ((enabled) && (pi_list.SelectedItems[0].Checked))
            {
                pi_a.Enabled = false;
                string pi_name = pi_list.SelectedItems[0].Text;
                if (Notepad_X.Plugin.Global.Plugins.AvailablePlugins.Exist(pi_name)) {
                        Plugin.AvailablePlugin a= Notepad_X.Plugin.Global.Plugins.AvailablePlugins.Find(pi_name);
                        if(a.Instance.HasSettings){
                            pi_s.Enabled=true;
                        }
                }
            }
            if ((enabled) && (!pi_list.SelectedItems[0].Checked))
            {
                pi_d.Enabled = false;
            }
        }

        private void pi_a_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in pi_list.SelectedItems)
            {
                item.Checked = true;
            }
        }

        private void pi_d_Click(object sender, EventArgs e)
        {

            foreach (ListViewItem item in pi_list.SelectedItems)
            {
                item.Checked = false;
            }
        }

        private void tl_a_Click(object sender, EventArgs e)
        {
            tl_list.SelectedItems[0].Checked = true;
        }

        private void tl_d_Click(object sender, EventArgs e)
        {
            tl_list.SelectedItems[0].Checked = false;
        }

        private void pi_r_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in pi_list.SelectedItems)
            {
                item.Checked = false;
                if (Notepad_X.Plugin.Global.Plugins.AvailablePlugins.Exist(item.SubItems[1].Text))
                {
                    Notepad_X.Plugin.Global.Plugins.ClosePlugin(item.SubItems[1].Text);
                } 
                System.IO.File.Delete(item.SubItems[1].Text);
            }
        }
        
        private void tl_r_Click(object sender, EventArgs e)
        {       
                //Delete Toolbar File
                tl_list.SelectedItems[0].Checked = false;
                System.IO.File.Delete(Main.System_ROOT + "\\Toolbars\\" +tl_list.SelectedItems[0].SubItems[1].Text);
                tl_list.SelectedItems[0].Remove();

        }

        private void tl_up_Click(object sender, EventArgs e)
        {
            //Switch positions
            int index = tl_list.SelectedItems[0].Index;
            ListViewItem tempitem = tl_list.Items[index -1];
            ListViewItem tempitem2 = tl_list.Items[index];

            tl_list.Items.RemoveAt(index);
            tl_list.Items.RemoveAt(index-1);
            tl_list.Items.Insert(index - 1, tempitem2);
            tl_list.Items.Insert(index, tempitem);
        }

        private void tl_down_Click(object sender, EventArgs e)
        {
            //Switch positions
            int index = tl_list.SelectedItems[0].Index;
            ListViewItem tempitem = tl_list.Items[index];
            ListViewItem tempitem2 = tl_list.Items[index+1];

            tl_list.Items.RemoveAt(index +1);
            tl_list.Items.RemoveAt(index);
            tl_list.Items.Insert(index, tempitem2);
            tl_list.Items.Insert(index+1, tempitem);
        }

        private void l_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool enabled = false;
            if (l_list.SelectedItems.Count > 0)
            {
                enabled = true;

                if (l_list.SelectedItems[0].Text.ToLower() == "english")
                {
                    enabled = false;
                }
            }
         
            l_rem.Enabled = enabled;
        }

        private void dl_update_Click(object sender, EventArgs e)
        {
            Download dll = new Download("http://notepad-x.sourceforge.net/files/downloads.php?list=all", (main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\downloads.xml", DownloadTypes.UpdateCheck);
            dll.DownloadCompletedEvent+=new Download.DownloadCompletedHandler(dll_DownloadCompletedEvent);
            dll.ShowDialog();
        }

        private void dl_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dl_list.SelectedItems.Count > 0)
            {
                dl_n.Text = dl_list.SelectedItems[0].SubItems[0].Text;
               
                if (dl_cat.SelectedIndex == 0 || dl_cat.SelectedIndex == 2)
                {
                    dl_d.Text = null;
                }else{

                    dl_d.Text = dl_list.SelectedItems[0].SubItems[1].Text;
                }
                dl_download.Enabled = true;
                dl_group.Visible = true;
            }else{
                dl_download.Enabled = false;
                dl_group.Visible = false;
                dl_n.Text = "";
                dl_d.Text = "";
            }
        }

        private void dl_cat_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (xmlDownloads != null)
            {
                switch (dl_cat.SelectedIndex)
                {
                    case 0:

                        if (tl_list_c_d.ListView != null)
                        {
                            dl_list.Columns.Remove(tl_list_c_d);
                        }
                        if (tl_list_c_v.ListView == null)
                        {
                            dl_list.Columns.Insert(2, tl_list_c_v);
                            tl_list_c_d.DisplayIndex = 2;
                        }
                        dl_additems("translations");
                        break;
                    case 1:

                        if (tl_list_c_d.ListView == null)
                        {
                            dl_list.Columns.Insert(1, tl_list_c_d);
                            tl_list_c_d.DisplayIndex = 1;
                        }
                        if (tl_list_c_v.ListView == null)
                        {
                            dl_list.Columns.Insert(3, tl_list_c_v);
                            tl_list_c_d.DisplayIndex = 3;
                        }
                        dl_additems("toolbars");
                        break;
                    case 2:
                        if (tl_list_c_d.ListView != null)
                        {
                            dl_list.Columns.Remove(tl_list_c_d);
                        }
                        if (tl_list_c_v.ListView != null)
                        {
                            dl_list.Columns.Remove(tl_list_c_v);
                        }
                        dl_additems("highlighters");
                        break;
                    case 3:
                        if (tl_list_c_d.ListView == null)
                        {
                            dl_list.Columns.Insert(1, tl_list_c_d);
                            tl_list_c_d.DisplayIndex = 1;
                        }
                        if (tl_list_c_v.ListView == null)
                        {
                            dl_list.Columns.Insert(3, tl_list_c_v);
                            tl_list_c_d.DisplayIndex = 3;
                        }
                        dl_additems("plugins");
                        break;
                    case 4:
                        if (tl_list_c_d.ListView == null)
                        {
                            dl_list.Columns.Insert(1, tl_list_c_d);
                            tl_list_c_d.DisplayIndex = 1;
                        }
                        if (tl_list_c_v.ListView == null)
                        {
                            dl_list.Columns.Insert(3, tl_list_c_v);
                            tl_list_c_d.DisplayIndex = 3;
                        }
                        dl_additems("iconpacks");
                        break;
                }
            }
        }
  
        private void dl_download_Click(object sender, EventArgs e)
        {
            if (dl_list.SelectedItems.Count > 0)
            {
                string url;
                if (dl_cat.SelectedIndex == 0 )
                {
                    url = dl_list.SelectedItems[0].SubItems[3].Text;
                }else if (dl_cat.SelectedIndex == 2){
                    url = dl_list.SelectedItems[0].SubItems[2].Text;
                }
                else
                {
                    url = dl_list.SelectedItems[0].SubItems[4].Text;
                }
                string dir = null;
                DownloadTypes type= DownloadTypes.Autoclose;
                string ext = null;
                switch (dl_cat.SelectedIndex)
                {
                    case 0:
                        dir = "Translations\\";
                        type = DownloadTypes.Translation;
                        ext = ".xml";
                        break;
                    case 1:
                        dir = "Toolbars\\";
                        type = DownloadTypes.Toolbar;
                        ext = ".xml";
                        break;
                    case 2:
                        dir = "Highlighters\\";
                        type = DownloadTypes.Highlighter;
                        ext = ".xshd";
                        break;
                    case 3:
                        dir = "Plugins\\";
                        type = DownloadTypes.Plugin;
                        ext = ".dll";
                        break;
                    case 4:
                        dir = "Icons\\";
                        type = DownloadTypes.IconPack;
                        ext = ".pack";
                        break;

                }
                bool ok = true;
                string path=(main.useAppDataFolder?main.System_AppData_FOLDER:Main.System_ROOT) + "\\" + dir + dl_list.SelectedItems[0].Text.Replace(" ","") + ext;

                if(System.IO.File.Exists(path)){
                    if (MessageBox.Show(path + " already exists do you want to update it ?","", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        if (FileInUse(path))
                        {
                            MessageBox.Show(path + " file is in use");
                            ok = false;
                        }
                    }else{
                        ok = false;
                    }
                }
                if(ok){

                    Download dll = new Download(url, path, type);
                    dll.DownloadCompletedEvent += new Download.DownloadCompletedHandler(dll_DownloadCompletedEvent);
                    dll.Show();
                }
                
            }
            else
            {
                dl_download.Enabled = false;
            }
            
        }

        void dll_DownloadCompletedEvent(object sender, DownloadCompleted e)
        {
            string[] hash;
            switch (e.DownloadType)
            {

                case DownloadTypes.List:
                    if (System.IO.Path.GetFileName(e.FilePath).ToLower() == "downloads.xml")
                    {
                        readDownloads(e.FilePath);
                    }
                    break;
                case DownloadTypes.Update:
                    string u_path = (main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Notepad-X-Install-" + u_latest.Text + ".exe";
                    if (System.IO.File.Exists(e.FilePath))
                    {
                        MessageBox.Show("Please close all Notepad X windows and continue with the installization of the update");
                        System.Diagnostics.Process.Start(u_path);
                    }
                    break;
                case DownloadTypes.UpdateCheck:
                    if (System.IO.Path.GetFileName(e.FilePath).ToLower() == "downloads.xml")
                    {
                        readDownloads(e.FilePath);
                    }
                    break;
                case DownloadTypes.Translation:
                    hash = Language.hashTranslation(e.FilePath);
                    if (hash != null)
                    {
                        panel.SelectTab(tab_g); 
                        if (!listContatins(l_list, hash[0]))
                        {
                            ListViewItem newItem = new ListViewItem();
                            newItem.Text = hash[0];
                            newItem.SubItems.Add(e.FilePath);
                            newItem.SubItems.Add(hash[1]); //author
                            newItem.SubItems.Add(hash[2]); //version
                            newItem.SubItems.Add(System.IO.Path.GetFileName(e.FilePath));
                            l_list.Items.Add(newItem);
                            newItem.Selected = true;
                        }else{
                            ListViewItem existItem = listGetItem(l_list, hash[0]);
                            //Update author
                            existItem.SubItems[2].Text = hash[1];
                            //Update version
                            existItem.SubItems[3].Text = hash[2];
                            existItem.Selected = true;
                        }
                       
                    }
                    break;
                case DownloadTypes.Toolbar:
                    hash = Toolbar.hashToolbar(e.FilePath);
                    if (hash != null)
                    {
                        panel.SelectTab(tab_tl); 
                        if (!listContatins(tl_list, hash[0]))
                        {
                            ListViewItem newItem = new ListViewItem();
                            newItem.Text = hash[0];
                            newItem.SubItems.Add(System.IO.Path.GetFileName(e.FilePath)); //path
                            newItem.SubItems.Add(hash[1]); //author 
                            newItem.SubItems.Add(hash[2]); //version 
                            newItem.SubItems.Add(hash[3]); //description 
                            tl_list.Items.Add(newItem);
                            newItem.Selected = true;
                        }
                        else
                        {
                            ListViewItem existItem = listGetItem(tl_list, hash[0]);
                            //Update author
                            existItem.SubItems[2].Text = hash[2];
                            //Update version
                            existItem.SubItems[3].Text = hash[2];
                            //Update description
                            existItem.SubItems[4].Text = hash[3];
                            existItem.Selected = true;
                        }
                        
                    }
                    break;
                case DownloadTypes.Plugin:
                    hash = Notepad_X.Plugin.Global.Plugins.hash(e.FilePath);

                    if (hash != null)
                    {
                        panel.SelectTab(tab_pi); 
                        if (!listContatins(pi_list, hash[0]))
                        {
                            ListViewItem newItem = new ListViewItem();
                            newItem.Text = hash[0]; //name
                            newItem.SubItems.Add(e.FilePath);
                            newItem.SubItems.Add(hash[1]); //author
                            newItem.SubItems.Add(hash[2]); //version
                            newItem.SubItems.Add(hash[3]); //description
                            pi_list.Items.Add(newItem);
                            newItem.Selected = true;
                        }
                        else
                        {
                            ListViewItem existItem = listGetItem(pi_list, hash[0]);
                            //Update author
                            existItem.SubItems[2].Text = hash[1];
                            //Update version
                            existItem.SubItems[3].Text = hash[2];
                            //Update description
                            existItem.SubItems[4].Text = hash[3];
                            existItem.Selected = true;
                        }
                       
                    }
                    break;
                case DownloadTypes.Highlighter:
                    //Need to extend!!
                    string key = highlighter_check(e.FilePath);
                    if (key != null)
                    {
                        panel.SelectTab(tab_h); 
                        updateHightlightersList();
                        h_changed = true;
                        
                    }

                    break;
                case DownloadTypes.IconPack:
                    
                    String Name = System.IO.Path.GetFileNameWithoutExtension(e.FilePath);
                    if (Packages.Unpack((main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Icons\\" + Name + ".pack", Main.System_ROOT + "\\Icons"))
                    {
                        try{System.IO.File.Delete(e.FilePath);}
                        catch{}
                        panel.SelectTab(tab_i);
                        hash = Settings.hashIconPack((main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Icons\\" + Name + ".xml");
                        if (hash != null)
                        {
                            if (!listContatins(i_list, hash[0]))
                            {
                                ListViewItem newItem = new ListViewItem();
                                newItem.Text = hash[0];
                                newItem.SubItems.Add(hash[1]); //author 
                                newItem.SubItems.Add(Name + ".xml"); //path
                                newItem.SubItems.Add(hash[2]); //version 
                                newItem.SubItems.Add(hash[3]); //description 
                                newItem.SubItems.Add(hash[4]); //link 
                                i_list.Items.Add(newItem);
                                newItem.Selected = true;
                            }
                            else
                            {
                                ListViewItem existItem = listGetItem(i_list, hash[0]);
                                //Update author
                                existItem.SubItems[1].Text = hash[1];
                                //Update version
                                existItem.SubItems[3].Text = hash[2];
                                //Update description
                                existItem.SubItems[5].Text = hash[3];
                                //Update link
                                existItem.SubItems[6].Text = hash[4];
                                existItem.Selected = true;
                            }

                        }  
                    }
                    
          
                    break;
            }
        }
        static bool FileInUse(string path)
        {
           try
           {
                //Just opening the file as open/create
                using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate))
                {
                    //If required we can check for read/write by using fs.CanRead or fs.CanWrite
                }
                return false;
            }
            catch 
            {
                //check if message is for a File IO
                
                   return true;
                //else
                // !throw;
            }
        }

        private void tl_download_Click(object sender, EventArgs e)
        {
            panel.SelectTab(tab_dl);
            dl_cat.SelectedIndex = dl_cat.FindStringExact(Main.app_language[Strings.Toolbars]);
            dl_list.Focus();
        }

        private void l_dll_Click(object sender, EventArgs e)
        {
            panel.SelectTab(tab_dl);
            dl_cat.SelectedIndex = dl_cat.FindStringExact(Main.app_language[Strings.Translation]);
            dl_list.Focus();
        }

        private void tl_add_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Notepad X Toolbars Files (*.xml)|*.xml";
            dialog.Title = "Add Toolbars";
            dialog.CheckFileExists = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                foreach (string file in dialog.FileNames)
                {
                    string[] hash = Toolbar.hashToolbar(file);
                    if (hash != null)
                    {
                        string path = (main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Toolbars\\" + System.IO.Path.GetFileName(file);
                        System.IO.File.Copy(file, path, true);
                        if (!listContatins(tl_list, hash[0]))
                        {
                            ListViewItem newItem = new ListViewItem();
                            newItem.Text = hash[0];
                            newItem.SubItems.Add(System.IO.Path.GetFileName(path)); //path
                            newItem.SubItems.Add(hash[1]); //author 
                            newItem.SubItems.Add(hash[2]); //version 
                            newItem.SubItems.Add(hash[3]); //description 
                            tl_list.Items.Add(newItem);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Invailid file");
                    }
                }
                }
            }

        private void l_add_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Notepad X Translations Files (*.xml)|*.xml";
            dialog.Title = "Add Translations";
            dialog.CheckFileExists = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                foreach (string file in dialog.FileNames)
                {
                    string[] hash = Language.hashTranslation(file);
                    if (hash != null)
                    {
                        string path = (main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Translations\\" + System.IO.Path.GetFileName(file);
                        System.IO.File.Copy(file, path, true);
                        if (!listContatins(l_list,hash[0])){
                            ListViewItem newItem = new ListViewItem();
                            newItem.Text = hash[0];
                            newItem.SubItems.Add(file);
                            newItem.SubItems.Add(hash[1]); //author 
                            newItem.SubItems.Add(hash[2]); //version
                            newItem.SubItems.Add(System.IO.Path.GetFileName(file));
                            l_list.Items.Add(newItem);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Invailid file");
                    }
                }
            }
        }

        private void h_dll_Click(object sender, EventArgs e)
        {
            panel.SelectTab(tab_dl);
            dl_cat.SelectedIndex = dl_cat.FindStringExact(Main.app_language[Strings.Highlighters]);
            dl_list.Focus();
        }

        private void pi_dll_Click(object sender, EventArgs e)
        {
            panel.SelectTab(tab_dl);
            dl_cat.SelectedIndex = dl_cat.FindStringExact(Main.app_language[Strings.Plugins]);
            dl_list.Focus();
        }

        private void l_r_Click(object sender, EventArgs e)
        {
            l_list.SelectedItems[0].Checked = false;
            System.IO.File.Delete((main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Translations\\" + l_list.SelectedItems[0].SubItems[4].Text);
            l_list.SelectedItems[0].Remove();
        }

        private void h_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool enabled = false;
            if(h_list.SelectedItems.Count>0){
                enabled = true;
            }
            h_rem.Enabled=enabled;
        }

        private void h_add_Click(object sender, EventArgs e)
        {

            
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Notepad X Hightlighter Files (*.xshd)|*.xshd";
            dialog.Title = "Add Hightlighters";
            dialog.CheckFileExists = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                foreach (string file in dialog.FileNames)
                {
                    //string[] hash = Language.hashTranslation(file);
                    //if (hash != nulHl)
                    //{
                    if(highlighter_check(file)!=null){
                    string path = (main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Highlighters\\" + System.IO.Path.GetFileName(file);
                    System.IO.File.Copy(file, path, true);

                    updateHightlightersList();
                    h_changed = true;
                    }else{
                         MessageBox.Show("Invailid file");
                    }
                
                }
            }
        }

        private void h_rem_Click(object sender, EventArgs e)
        {
            //delete
            foreach (ListViewItem item in h_list.SelectedItems)
            {
                if (System.IO.File.Exists(item.SubItems[1].Text))
                {
                    System.IO.File.Delete(item.SubItems[1].Text);
                    h_changed = true;
                }
            }
            updateHightlightersList();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(h_list.SelectedItems[0].SubItems[1].Text);
        }

        private void tl_br_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start((main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Toolbars\\");
        }

        private void h_br_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start((main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Highlighters\\");
        }

        private void pi_br_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start((main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Plugins\\");
        }

        private void i_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool enabled = false;
            i_rem.Enabled = false;
            if (i_list.SelectedItems.Count > 0)
            {
                enabled = true;
                ListViewItem selectedItem = i_list.SelectedItems[0];
                i_i_l.Text = selectedItem.SubItems[5].Text;
                i_i_v.Text = selectedItem.SubItems[3].Text;
                i_i_d.Text = selectedItem.SubItems[4].Text;
                if (i_list.SelectedItems[0].SubItems[1].Text.ToLower() == "default")
                {
                    i_rem.Enabled = false;
                }
                else
                {
                    i_rem.Enabled = true;
                }
            }
            i_i_group.Visible = enabled;
          
            
        }

        private void pi_add_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Notepad X Plugin Files (*.dll)|*.dll";
            dialog.Title = "Add Plugins";
            dialog.CheckFileExists = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                foreach (string file in dialog.FileNames)
                {
                    string[] hash = Notepad_X.Plugin.Global.Plugins.hash(file);
                    if (hash != null)
                    {
                        string path = (main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Plugins\\" + System.IO.Path.GetFileName(file);
                        if (path.ToLower() != file.ToLower())
                        {
                            System.IO.File.Copy(file, path, true);
                        }
                        if (!listContatins(pi_list, hash[0]))
                        {
                            ListViewItem newItem = new ListViewItem();

                            newItem.Text = hash[0]; //name
                            newItem.SubItems.Add(file);
                            newItem.SubItems.Add(hash[1]); //author
                            newItem.SubItems.Add(hash[2]); //version
                            newItem.SubItems.Add(hash[3]); //description

                            pi_list.Items.Add(newItem);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Invailid file");
                    }
                }
            }
        }

        private void i_dl_Click(object sender, EventArgs e)
        {
            panel.SelectTab(tab_dl);
            dl_cat.SelectedIndex = dl_cat.FindStringExact(Main.app_language[Strings.Icons]);
            dl_list.Focus();
        }

        private void i_br_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Main.System_ROOT + "\\Icons\\");
        }

        private void i_rem_Click(object sender, EventArgs e)
        {
            String FileName = System.IO.Path.GetFileNameWithoutExtension(i_list.SelectedItems[0].SubItems[2].Text);
            if (System.IO.Directory.Exists((main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Icons\\" + FileName))
            {
                System.IO.Directory.Delete((main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Icons\\" + FileName, true);
            }
            System.IO.File.Delete((main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Icons\\" + i_list.SelectedItems[0].SubItems[2].Text);
            i_list.Items.Remove(i_list.SelectedItems[0]);
            if (i_list.Items.Count == 1)
            {
                i_list.Items[0].Checked = true;
            }
        }

        private void u_dll_Click(object sender, EventArgs e)
        {
            Download dll = new Download(latesturl, (main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Notepad-X-Install-" + u_latest.Text + ".exe", DownloadTypes.Update);
            dll.ShowDialog(this);
        }

        private void e_s_edit_Click(object sender, EventArgs e)
        {
            Extensions ext = new Extensions(main);
            string[] result = ext.EditDialog(e_s_list.SelectedItems[0].Text, e_s_list.SelectedItems[0].SubItems[1].Text);
            if (result[0] != null)
            {
                e_s_list.SelectedItems[0].Text = result[0];
                e_s_list.SelectedItems[0].SubItems[1].Text = result[1];
            }
        }

        private void e_s_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            Boolean enabled = false;
            if (e_s_list.SelectedItems.Count > 0)
            {
                enabled = true;
            }
            e_s_edit.Enabled = enabled;
            e_s_rem.Enabled = enabled;
        }

        private void e_open_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            Boolean enabled = false;
            if (e_o_list.SelectedItems.Count > 0)
            {
                enabled = true;
            }
            e_o_edit.Enabled = enabled;
            e_o_rem.Enabled = enabled;
        }

        private void e_o_edit_Click(object sender, EventArgs e)
        {
            Extensions ext = new Extensions(main);
            string[] result = ext.EditDialog(e_o_list.SelectedItems[0].Text, e_o_list.SelectedItems[0].SubItems[1].Text);
            if (result[0] != null)
            {
                e_o_list.SelectedItems[0].Text = result[0];
                e_o_list.SelectedItems[0].SubItems[1].Text = result[1];
            }
        }

        private void e_s_add_Click(object sender, EventArgs e)
        {
            Extensions ext = new Extensions(main);
            string[] result = ext.AddDialog();
            if (!String.IsNullOrEmpty(result[0]))
            {
                ListViewItem newItem = new ListViewItem();
                newItem.Text = result[0];
                newItem.SubItems.Add(result[1]);
                e_s_list.Items.Add(newItem);
            }
        }

        private void e_o_add_Click(object sender, EventArgs e)
        {
            Extensions ext = new Extensions(main);
            string[] result = ext.AddDialog();
            if (!String.IsNullOrEmpty(result[0]))
            {
                ListViewItem newItem = new ListViewItem();
                newItem.Text = result[0];
                newItem.SubItems.Add(result[1]);
                e_o_list.Items.Add(newItem);
            }
        }

        private void e_o_rem_Click(object sender, EventArgs e)
        {
            e_o_list.Items.Remove(e_o_list.SelectedItems[0]);
        }

        private void e_s_rem_Click(object sender, EventArgs e)
        {
            e_s_list.Items.Remove(e_s_list.SelectedItems[0]);
        }

        private void i_i_l_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(i_i_l.Text);
            }
            catch (Exception)
            {
            }
        }

        private void sh_e_clear_Click(object sender, EventArgs e)
        {
            sh_list.SelectedItems[0].SubItems[1].Text = null;
            sh_selected();
        }

        private void i_add_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Notepad X IconPack Files (*.pack)|*.pack";
            dialog.Title = "Add Iconpacks";
            dialog.CheckFileExists = true;

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                foreach (string file in dialog.FileNames)
                {
                    string packfile = System.IO.Path.GetFileNameWithoutExtension(file);

                    Packages.Unpack(file, (main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Icons");

                    String IconPackFile = (main.useAppDataFolder ? main.System_AppData_FOLDER : Main.System_ROOT) + "\\Icons\\" + packfile + ".xml";

                    if (System.IO.File.Exists(IconPackFile))
                    {
                   
                        string[] hash = Settings.hashIconPack(IconPackFile);
                        if (hash != null)
                        {
                            foreach (ListViewItem i in i_list.Items)
                            {
                                i.Selected = false;
                            }
                            if (!listContatins(i_list, hash[0]))
                            {
                                ListViewItem newItem = new ListViewItem();
                                newItem.Name = hash[0];
                                newItem.Text = hash[0];
                                newItem.SubItems.Add(hash[1]); //author
                                newItem.SubItems.Add(IconPackFile); //path
                                newItem.SubItems.Add(hash[2]); //version 
                                newItem.SubItems.Add(hash[3]); //description 
                                newItem.SubItems.Add(hash[4]); //link 
                                i_list.Items.Add(newItem);
                                newItem.Selected = true;
                            }
                            else
                            {
                                i_list.Items[hash[0]].Selected = true;
                            }
                            i_list.Focus();
                        }
                    }
                }
            }
        }

        private void sh_default_Click(object sender, EventArgs e)
        {
            //clear all the shortcuts
            for (int i = 0; i < sh_list.Items.Count; i++) {
                sh_list.Items[i].SubItems[1].Text = null;
       
            }

            //Call the setShortcutKeys for each control and set the shortcut
            setShortcutKeys("Menu_File_New", "Ctrl + N");
            setShortcutKeys("Menu_File_Open", "Ctrl + O");
            setShortcutKeys("Menu_File_Save", "Ctrl + S");
            setShortcutKeys("Menu_File_SaveAs", "Ctrl + Shift + C");
            setShortcutKeys("Menu_File_Print", "Ctrl + P");
            setShortcutKeys("Menu_File_Exit", "Alt + F4");
            setShortcutKeys("Menu_Edit_Copy", "Ctrl + C");
            setShortcutKeys("Menu_Edit_Cut", "Ctrl + X");
            setShortcutKeys("Menu_Edit_Paste", "Ctrl + P");
            setShortcutKeys("Menu_Edit_Redo", "Ctrl + Y");
            setShortcutKeys("Menu_Edit_Undo", "Ctrl + Z");
            setShortcutKeys("Menu_Edit_Find", "Ctrl + F");
            setShortcutKeys("Menu_Edit_SelectAll", "Ctrl + A");
            setShortcutKeys("Tabs_Select_0", "Ctrl + 1");
            setShortcutKeys("Tabs_Select_1", "Ctrl + 2");
            setShortcutKeys("Tabs_Select_2", "Ctrl + 3");
            setShortcutKeys("Tabs_Select_3", "Ctrl + 4");
            setShortcutKeys("Tabs_Select_4", "Ctrl + 5");
            setShortcutKeys("Tabs_Select_5", "Ctrl + 6");
            setShortcutKeys("Tabs_Select_6", "Ctrl + 7");
            setShortcutKeys("Tabs_Select_7", "Ctrl + 8");
            setShortcutKeys("Tabs_Select_8", "Ctrl + 9");
            setShortcutKeys("Tabs_Select_9", "Ctrl + 0");
            setShortcutKeys("Menu_Tabs_Close", "Ctrl + W");
            setShortcutKeys("Menu_Tabs_New", "Ctrl + T");
        }
        //Set the shortcut for current item by name , used at set default shortcuts function
        private void setShortcutKeys(String Name, String shortcut)
        {
            for (int i = 0; i < sh_list.Items.Count; i++)
            {
                //Chech if the current item index is out control name (SubItems[2] containts the control name)
                if (sh_list.Items[i].SubItems[2].Text == Name)
                {
                    sh_list.Items[i].SubItems[1].Text = shortcut;
                    break;
                }
                
            }
        }
        private void g_backgroundColor_CheckedChanged(object sender, EventArgs e) {
            g_backgroundColor_choose.Enabled = !g_backgroundColor.Checked;
        }

        private void g_fontColor_CheckedChanged(object sender, EventArgs e) {
            g_fontColor_choose.Enabled = !g_fontColor.Checked;
        }

        private void g_backgroundColor_choose_Click(object sender, EventArgs e) {
            ColorDialog Dialog = new ColorDialog();
            Dialog.Color = g_backgroundColor_preview.BackColor;

            if (Dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                g_backgroundColor_preview.BackColor = Dialog.Color;
            }
        }

        private void g_fontColor_choose_Click(object sender, EventArgs e) {
            ColorDialog Dialog = new ColorDialog();
            Dialog.Color = g_fontColor_preview.BackColor;

            if (Dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                g_fontColor_preview.BackColor = Dialog.Color;
            }
        }

        private void pi_s_Click(object sender, EventArgs e) {
            string pi_name = pi_list.SelectedItems[0].Text;
            if (Notepad_X.Plugin.Global.Plugins.AvailablePlugins.Exist(pi_name)) {
                Plugin.AvailablePlugin a = Notepad_X.Plugin.Global.Plugins.AvailablePlugins.Find(pi_name);
                if (a.Instance.HasSettings) {
                    a.Instance.Settings();
                }
            }
        }
    }
 }

   